home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / START HERE - Box / BoxShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  7.8 KB  |  301 lines  |  [TEXT/MPCC]

  1. // BoxShell.c - QuickDraw 3d routines
  2. //
  3. // This is box, the QuickDraw 3D starter program.  Written for the
  4. // Getting started with QuickDraw 3D Develop article.  This app does not have 
  5. // graceful error handling - it's putpose is to illustrate a very basic QuickDraw 
  6. // 3D program.
  7. //
  8. // Nick Thompson - January 6th 1995
  9. // 
  10. // ©1994-95 Apple computer Inc., All Rights Reserved
  11. //
  12.  
  13. // system headers
  14. #include <Desk.h>
  15. #include <Dialogs.h>
  16. #include <DiskInit.h>
  17. #include <Fonts.h>
  18. #include <Menus.h>
  19. #include <PictUtil.h>
  20. #include <QDOffScreen.h>
  21. #include <QuickDraw.h>
  22. #include <SegLoad.h>
  23. #include <StandardFile.h>
  24. #include <TextEdit.h>
  25.  
  26. // for QuickDraw 3D
  27. #include "QD3D.h"
  28. #include "QD3DMath.h"
  29. #include "QD3DDrawContext.h"
  30. #include "QD3DShader.h"
  31. #include "QD3DTransform.h"
  32. #include "QD3DGroup.h"
  33.  
  34.  
  35. #include "BoxShell.h"
  36. #include "Box3DSupport.h"
  37.  
  38. //-------------------------------------------------------------------------------------------
  39.  
  40. struct _documentRecord {
  41.     TQ3ViewObject    fView ;                    // the view for the scene
  42.     TQ3GroupObject    fModel ;                // object in the scene being modelled
  43.     TQ3StyleObject    fInterpolation ;        // interpolation style used when rendering
  44.     TQ3StyleObject    fBackFacing ;            // whether to draw shapes that face away from the camera
  45.     TQ3StyleObject    fFillStyle ;            // whether drawn as solid filled object or decomposed to components
  46.     TQ3Matrix4x4        fRotation;                // the transform for the model
  47. };
  48.  
  49. typedef struct _documentRecord DocumentRec, *DocumentPtr, **DocumentHdl ;
  50.  
  51.  
  52. //-------------------------------------------------------------------------------------------
  53. // function prototypes
  54.  
  55. static void         InitToolbox( void ) ;
  56. static void         MainEventLoop( void ) ;
  57. static void         HandleKeyPress(EventRecord *event) ;
  58. static void         HandleOSEvent(EventRecord *event) ;
  59. void InitDocumentData( DocumentPtr theDocument ) ;
  60. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  61. void DisposeDocumentData( DocumentPtr theDocument) ;
  62.  
  63.  
  64. //-------------------------------------------------------------------------------------------
  65. //
  66.  
  67. Boolean         gQuitFlag         = false ;
  68. WindowPtr        gMainWindow        = nil ;
  69. DocumentRec        gDocument ;
  70.  
  71. //-------------------------------------------------------------------------------------------
  72. // main()
  73. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  74. // and enter the main event loop.  On exit from the main event loop, we want to call
  75. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  76.  
  77. void main(void)
  78. {
  79.     TQ3Status    myStatus;
  80.     Rect        rBounds = { 50, 50, 450, 450 } ;
  81.     Str255        title = "\pSpinning Box" ;
  82.  
  83.     InitToolbox() ;
  84.     
  85.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  86.     myStatus = Q3Initialize();
  87.  
  88.     if ( myStatus == kQ3Failure )
  89.         DebugStr("\pErInitialize returned failure.");            
  90.  
  91.     // set up our globals
  92.     gQuitFlag = false ;
  93.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  94.  
  95.     InitDocumentData( &gDocument ) ;
  96.     
  97.     MainEventLoop();
  98.     
  99.     DisposeDocumentData( &gDocument ) ;
  100.     
  101.     //    Close our connection to the QuickDraw 3D library
  102.     myStatus = Q3Exit();
  103.     if ( myStatus == kQ3Failure )
  104.         DebugStr("\pErExit returned failure.");
  105.     
  106. }
  107.  
  108. //-------------------------------------------------------------------------------------------
  109. //
  110.  
  111. void InitDocumentData( DocumentPtr theDocument ) 
  112. {
  113.     // sets up the 3d data for the scene
  114.     // Create view for QuickDraw 3D.
  115.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  116.  
  117.     // the main display group:
  118.     theDocument->fModel = MyNewModel() ;
  119.  
  120.     // the drawing styles:
  121.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  122.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth ) ;
  123.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  124.  
  125.     // set the rotation matrix the identity matrix
  126.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);        
  127. }
  128.  
  129. void DisposeDocumentData( DocumentPtr theDocument)
  130. {
  131.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  132.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  133.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  134.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  135.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  136.  
  137. }
  138. //-----------------------------------------------------------------------------
  139. // 
  140.  
  141. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  142. {    
  143.     Q3View_StartRendering(theDocument->fView );
  144.     do {
  145.         Q3Style_Submit( theDocument->fInterpolation, theDocument->fView );
  146.         Q3Style_Submit( theDocument->fBackFacing, theDocument->fView );
  147.         Q3Style_Submit( theDocument->fFillStyle, theDocument->fView );
  148.         Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView );
  149.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  150.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  151.     return kQ3Success ;
  152. }
  153.  
  154.  
  155. //----------------------------------------------------------------------------------
  156.  
  157. //-------------------------------------------------------------------------------------------
  158. //
  159.  
  160. short HiWrd(long aLong)
  161. {
  162.     return    (((aLong) >> 16) & 0xFFFF) ;
  163. }
  164.  
  165. //-------------------------------------------------------------------------------------------
  166. //
  167.  
  168. short LoWrd(long aLong)
  169. {
  170.     return    ((aLong) & 0xFFFF) ;
  171.  
  172. }
  173.  
  174. //-------------------------------------------------------------------------------------------
  175. //
  176.  
  177. void InitToolbox()
  178. {
  179.     Handle        menuBar = nil;
  180.  
  181.     MaxApplZone() ;
  182.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  183.     
  184.     InitGraf( &qd.thePort );
  185.     InitFonts();
  186.     InitWindows();
  187.     InitCursor();
  188.  
  189.     FlushEvents( everyEvent, 0 ) ;
  190.     // initialize application globals
  191.     
  192.     gQuitFlag = false;
  193.     
  194. }
  195.  
  196.  
  197. //-------------------------------------------------------------------------------------------
  198. //
  199. void MainEventLoop()
  200. {
  201.     EventRecord     event;
  202.     WindowPtr       window;
  203.     short           thePart;
  204.     Rect            screenRect, updateRect;
  205.     Point            aPoint = {100, 100};    
  206.  
  207.     while( !gQuitFlag )
  208.     {
  209.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  210.         {
  211.  
  212.             switch (event.what) {
  213.                 case mouseDown:
  214.                 
  215.                     thePart = FindWindow( event.where, &window );
  216.                     
  217.                     switch( thePart ) {
  218.                         case inMenuBar: 
  219.                             break;
  220.                         
  221.                         case inDrag:
  222.                     
  223.                             screenRect = (**GetGrayRgn()).rgnBBox;
  224.                             DragWindow( window, event.where, &screenRect );
  225.                             break ;
  226.                     
  227.                         case inContent:
  228.                     
  229.                             if (window != FrontWindow())
  230.                                 SelectWindow( window );
  231.                             break ;
  232.                     
  233.                         case inGoAway:
  234.                             if (TrackGoAway( window, event.where )) {
  235.                                 DisposeWindow ( window );
  236.                                 gQuitFlag = true;
  237.  
  238.                             }
  239.                             break ;
  240.                             
  241.                         default:
  242.                             break ;
  243.                     }
  244.                     break ;
  245.                             
  246.                         
  247.                 case updateEvt:
  248.                 
  249.                     window = (WindowPtr)event.message;
  250.                     updateRect = (**(window->visRgn)).rgnBBox;
  251.                     SetPort( window ) ;
  252.                     BeginUpdate( window );
  253.                     DocumentDraw3DData( &gDocument ) ;
  254.                     EndUpdate( window );
  255.                     break ;
  256.                     
  257.                 case keyDown:
  258.                 case autoKey:
  259.                     HandleKeyPress(&event);
  260.                     break;
  261.                     
  262.                 case diskEvt:
  263.                     if ( HiWrd(event.message) != noErr ) 
  264.                         (void) DIBadMount(aPoint, event.message);
  265.                     break;
  266.                     
  267.                 case osEvt:
  268.                 case activateEvt:
  269.                     break;
  270.  
  271.  
  272.             }
  273.         }
  274.         else {
  275.             // we received a null event, rotate the cube
  276.             TQ3Matrix4x4    tmp;
  277.             Rect        theRect = ((GrafPtr)gMainWindow)->portRect ;
  278.             
  279.             SetPort((GrafPtr)gMainWindow) ;
  280.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  281.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  282.  
  283.             InvalRect( &theRect ) ;
  284.         }
  285.     }
  286. }
  287.  
  288.  
  289. //-------------------------------------------------------------------------------------------
  290. //
  291. void HandleKeyPress(EventRecord *event)
  292. {
  293. #pragma unused ( event )
  294. }
  295.  
  296. //-------------------------------------------------------------------------------------------
  297. //
  298.  
  299.  
  300.  
  301.